//
//  ViewController.swift
//  eric
//
//  Created by 杜承玖 on 2/9/15.
//  Copyright (c) 2015 redianying. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var timeLabel: UILabel?
    var timeButtons:[UIButton]?
    var startStopButton:UIButton?
    var clearButton: UIButton?
    let timeButtonInfos = [("1 min",60),("min",180),("5 min",300),("s",1)]
    var remainingSeconds:Int = 0 {
        willSet(newSeconds){
          let mins = newSeconds/60
          let seconds = newSeconds%60
            self.timeLabel!.text = NSString(format:"%02d:%02d","%02d:%02d")
        }
    }
    var isCounting:Bool = false{
        willSet(newValue){
            if newValue {
                timer = NSTimer.scheduledTimerWithTimeInterval(1,target:self,selector:"updateTimer:",userInfo:"",repeats:true)
            }else{
              timer?.invalidate()
              timer = nil
            }
            setSettingButtonsEnabled(!newValue)
        }
    }
    var timer:NSTimer?
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = UIColor.whiteColor()
        setupTimeLabel()
        setUptimeButtons()
        setupActionButtons()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupTimeLabel(){
      timeLabel = UILabel()
        timeLabel!.text = "00:00"
        timeLabel!.textColor = UIColor.whiteColor()
        timeLabel!.font = UIFont(name: "",size:80)
        timeLabel!.backgroundColor = UIColor.blackColor()
        timeLabel!.textAlignment = NSTextAlignment.Center
        
        self.view.addSubview(timeLabel!)
    }
    
    func setUptimeButtons(){
      var buttons = [UIButton]()
        for(index,(title,_)) in enumerate(timeButtonInfos){
            let button:UIButton = UIButton()
            button.tag = index
            button.setTitle("\(title)",forState:UIControlState.Normal)
            
            button.backgroundColor = UIColor.orangeColor()
            button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
            button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
           
            button.addTarget(self, action: "timeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
            
            buttons.append(button)
            self.view.addSubview(button)
        }
        timeButtons = buttons
        
    }
    
    func setupActionButtons(){
      startStopButton = UIButton()
      startStopButton!.backgroundColor = UIColor.redColor()
      startStopButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
      startStopButton!.setTitleColor(UIColor.blackColor(), forState:UIControlState.Normal)
      startStopButton!.setTitle("on/stop",forState:UIControlState.Normal)
      startStopButton!.addTarget(self, action: "startStopButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
      
        
     self.view.addSubview(startStopButton!)
      
     clearButton = UIButton()
     clearButton!.backgroundColor = UIColor.redColor()
     clearButton!.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
     clearButton!.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
     clearButton!.setTitle("reset" , forState:UIControlState.Normal)
     clearButton!.addTarget(self, action: "clearButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
     
     self.view.addSubview(clearButton!)
     
     
        
   
    }
    
    func startStopButtonTapped(sender:UIButton){
      isCounting = !isCounting
    }
    
    func clearButtonTapped(sender:UIButton){
        //println("")
        remainingSeconds = 0
    }
    
    func timeButtonTapped(sender: UIButton){
        let (_,seconds ) = timeButtonInfos[sender.tag]
        remainingSeconds += seconds
    }
    
    
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        timeLabel!.frame = CGRectMake(10,40, self.view.bounds.size.width - 20, 120)
        
        let gap = (self.view.bounds.size.width - 20 - (CGFloat(timeButtons!.count)*64))/CGFloat(timeButtons!.count - 1)
        
        for(index,button) in enumerate(timeButtons!){
          let buttonLeft = 10 + (64 + gap) * CGFloat(index)
          button.frame = CGRectMake(buttonLeft, self.view.bounds.size.height -  120, 64,44)
        
        }
        
        startStopButton!.frame = CGRectMake(10, self.view.bounds.size.height - 60, self.view.bounds.size.width-120, 44)
        
        clearButton!.frame = CGRectMake(10+self.view.bounds.size.width - 100, self.view.bounds.size.height  - 60, 80,44)
        
        
    }
    
    
    func setSettingButtonsEnabled(enabled: Bool){
        for button in self.timeButtons! {
          button.enabled = enabled
            button.alpha = enabled ? 1.0 : 0.3
         
        }
        clearButton!.enabled = enabled
        clearButton!.alpha = enabled ? 1.0 : 0.3
    }
    
    func updateTimer(timer:NSTimer){
      remainingSeconds -= 1
      
        if remainingSeconds <= 0{
          self.timeLabel?.text = "00:00"
          self.remainingSeconds = 0
          
          let alert = UIAlertView()
          alert.title = "over"
          alert.message = ""
          alert.addButtonWithTitle("ok")
          alert.show()
        }
    }
}

